package org.jeecg.modules.mr.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.arc.service.IArcCustomersService;
import org.jeecg.modules.mr.entity.MrRecords;
import org.jeecg.modules.mr.service.IMrRecordsService;
import org.jeecg.modules.mr.vo.ChangesRecordVo;
import org.jeecg.modules.mr.vo.ReadRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 抄表记录
 * @Author: jeecg-boot
 * @Date:   2022-02-03
 * @Version: V1.0
 */
@Api(tags="抄表记录")
@RestController
@RequestMapping("/mr/mrRecords")
@Slf4j
public class MrRecordsController extends JeecgController<MrRecords, IMrRecordsService> {
	@Autowired
	private IMrRecordsService mrRecordsService;

	@Autowired
	private IArcCustomersService arcCustomersService;

	 @Autowired
	 private RedisUtil redisUtil;


	/**
	 * 分页列表查询
	 *
	 * @param mrRecords
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "抄表记录-分页列表查询")
	@ApiOperation(value="抄表记录-分页列表查询", notes="抄表记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(MrRecords mrRecords,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		mrRecords.setDelFlag(0);
		mrRecords.setIsVaild(1);
		QueryWrapper<MrRecords> queryWrapper = QueryGenerator.initQueryWrapper(mrRecords, req.getParameterMap());
		Page<MrRecords> page = new Page<MrRecords>(pageNo, pageSize);
		IPage<MrRecords> pageList = mrRecordsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 待审查询
	  *
	  * @param pendingRecordVo
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "待审查询")
	 @ApiOperation(value="待审查询", notes="待审查询")
	 @GetMapping(value = "/pendingList")
	 public Result<?> pendingList(MrRecords pendingRecordVo,
								  HttpServletRequest req) {
		 //继续- 未抄、已抄表未审核
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 if(null != sysUser && !"admin".equals(sysUser.getUsername())){
//			 pendingRecordVo.setReaderId(sysUser.getUsername());
		 }
		 //待审
		 pendingRecordVo.setIsPassed("0");
		 List<MrRecords>mrList = mrRecordsService.queryPendingRecords(pendingRecordVo);

		 IPage<MrRecords> pageList=new Page<>();
		 pageList.setRecords(mrList);
		 return Result.OK(pageList);
	 }

	 /**
	  * 水量修改查询
	  *
	  * @param pendingRecordVo
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "水量修改")
	 @ApiOperation(value="水量修改", notes="水量修改")
	 @GetMapping(value = "/changeList")
	 public Result<?> changeList(MrRecords pendingRecordVo,
								  HttpServletRequest req) {
		 //已抄、已审、未审
		 if(oConvertUtils.isEmpty(pendingRecordVo.getCusCode())){
			 //return Result.error("户号必须填写");
			 IPage<MrRecords> pageList=new Page<>();
			 return Result.OK(pageList);
		 }
		 //已抄
		 pendingRecordVo.setIsPassed(null);
		 List<MrRecords>mrList = mrRecordsService.queryChangeRecords(pendingRecordVo);

		 IPage<MrRecords> pageList=new Page<>();
		 pageList.setRecords(mrList);
		 return Result.OK(pageList);
	 }

	/**
	 *   添加
	 *
	 * @param mrRecords
	 * @return
	 */
	@AutoLog(value = "抄表记录-添加")
	@ApiOperation(value="抄表记录-添加", notes="抄表记录-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody MrRecords mrRecords) {
		mrRecordsService.save(mrRecords);
		return Result.OK("添加成功！");
	}
	/**
	 *   手动抄表提交
	 *
	 * @param recordVo
	 * @return
	 */
	@AutoLog(value = "抄表记录-添加")
	@ApiOperation(value="抄表记录-添加", notes="抄表记录-添加")
	@PostMapping(value = "/readManual")
	public Result<?> readManual(@RequestBody ReadRecordVo recordVo) {
		return mrRecordsService.readManual(recordVo);
	}

	 /**
	  *   手动远传抄表提交
	  *
	  * @param recordVo
	  * @return
	  */
	 @AutoLog(value = "远传抄表记录-添加")
	 @ApiOperation(value="远传抄表记录-添加", notes="远传抄表记录-添加")
	 @PostMapping(value = "/readRemoteManual")
	 public Result<?> readRemoteManual(@RequestBody ReadRecordVo recordVo) {
		 if(StringUtils.isEmpty(recordVo.getCustomerId())){
			 return Result.error("请选择远传水表");
		 }
		 return mrRecordsService.readRemoteManual(recordVo);
	 }

	/**
	 *   手动抄表删除
	 *
	 * @param recordVo
	 * @return
	 */
	@AutoLog(value = "抄表记录-删除")
	@ApiOperation(value="抄表记录-删除", notes="抄表记录-删除")
	@PostMapping(value = "/readDelManual")
	public Result<?> readDelManual(@RequestBody ReadRecordVo recordVo) {
		return mrRecordsService.readDelManual(recordVo);
	}

	/**
	 *  编辑
	 *
	 * @param mrRecords
	 * @return
	 */
	@AutoLog(value = "抄表记录-编辑")
	@ApiOperation(value="抄表记录-编辑", notes="抄表记录-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody MrRecords mrRecords) {
		mrRecordsService.updateById(mrRecords);
		return Result.OK("编辑成功!");
	}
	/**
	 *  水量修改
	 * 水量修改-对已审核（已计量已计费已生成欠费记录）的进行回头修改，没有审核的则可以直接在抄表录入直接修改，已收费的则不出现
	 * @param mrRecords
	 * @return
	 */
	@AutoLog(value = "水量修改-保存")
	@ApiOperation(value="水量修改-编辑", notes="水量修改-编辑")
	@PutMapping(value = "/changeEdit")
	public Result<?> changeEdit(@RequestBody MrRecords mrRecords) {
		ChangesRecordVo cvo=new ChangesRecordVo();
		cvo.setRecordId(mrRecords.getId());
		cvo.setNewEndScale(mrRecords.getEndScale());
		cvo.setNewAmount(mrRecords.getAmount());
		return	mrRecordsService.changesAmount(cvo);

	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "抄表记录-通过id删除")
	@ApiOperation(value="抄表记录-通过id删除", notes="抄表记录-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		mrRecordsService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "抄表记录-批量删除")
	@ApiOperation(value="抄表记录-批量删除", notes="抄表记录-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.mrRecordsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "抄表记录-通过id查询")
	@ApiOperation(value="抄表记录-通过id查询", notes="抄表记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		MrRecords mrRecords = mrRecordsService.getById(id);
		if(mrRecords==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mrRecords);
	}

	/**
	 *审核
	 *
	 * @param dataParam
	 * @return
	 */
	@AutoLog(value = "审核")
	@ApiOperation(value="审核", notes="审核")
	@PostMapping(value = "/auditsBatch")
	public Result<Object> auditsBatch(@RequestBody JSONObject dataParam) throws ParseException {
		/**String ids,Integer isPass,String note*/

		String ids=dataParam.getString("ids");
		if(oConvertUtils.isNotEmpty(ids)){
			String batchKey="";
			if(ids.length()>60){
				 batchKey=ids.substring(0,60);
			}else if(ids.length()>30){
				batchKey=ids.substring(0,30);
			}else if(ids.length()>10){
				batchKey=ids.substring(0,10);
			}
			if(oConvertUtils.isNotEmpty(batchKey)) {
				Object bo = redisUtil.get(batchKey);
				if (null != bo) {
					return Result.error("已在审核中，请耐心等待");
				}
				redisUtil.set(batchKey, "auditing", 5 * 60);
			}
		}

		return	mrRecordsService.auditsBatch(
				Arrays.asList(ids.split(","))
				,dataParam.getInteger("isPass"),dataParam.getString("note"));
	}



    /**
    * 导出excel
    *
    * @param request
    * @param mrRecords
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MrRecords mrRecords) {
        return super.exportXls(request, mrRecords, MrRecords.class, "抄表记录");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MrRecords.class);
    }

}
